package com.woniuxy.yoga.business.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.business.model.form.businesspayment.BusinessPagePayForm;
import com.woniuxy.yoga.business.model.form.businesspayment.SelectPagePaymentForm;
import com.woniuxy.yoga.business.model.param.businesspayment.BusinessPagePayParam;
import com.woniuxy.yoga.business.model.param.businesspayment.SelectPagePaymentParam;
import com.woniuxy.yoga.business.service.BusinessPaymentService;
import com.woniuxy.yoga.commons.alipay.util.AlipayUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import com.woniuxy.yoga.commons.web.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 商家入驻缴费表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年12月02日
 */
@RestController
@RequestMapping("/businessPayment")
@Api(tags = "平台入驻费相关接口")
public class BusinessPaymentController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private BusinessPaymentService businessPaymentService;

    /**
     * 分页查询商家入驻缴费列表
     * @param form
     * @param authen_token
     * @return
     */
    @PostMapping("/selectPagePayment.bu")
    @ApiOperation("商家查询入驻费用")
    @YogaLog("商家查询入驻费用")
    public Result<PageInfo> selectPagePayment(@Valid @RequestBody SelectPagePaymentForm form, @RequestHeader String authen_token){
        String refreshToken = redisUtil.get(authen_token);
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        SelectPagePaymentParam param = BeanUtil.toBean(form, SelectPagePaymentParam.class);
        param.setAccountId(businessId);
        PageInfo pageInfo = businessPaymentService.selectPagePayment(param);
        return Result.getResult(PageInfo.class).setMessage("分页查询入驻缴费成功").setData(pageInfo);
    }

    /**
     * 响应给商家支付页面
     * @param form
     * @param authen_token
     * @return
     * @throws Exception
     */
    @PostMapping("/businessPagePay.bu")
    @ApiOperation("商家支付入驻费用")
    @YogaLog("商家支付入驻费用")
    public Result<String> businessPagePay(@Valid @RequestBody BusinessPagePayForm form, @RequestHeader("authen_token") String authen_token) throws Exception {
        String refreshToken = redisUtil.get(authen_token);
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        BusinessPagePayParam param = BeanUtil.toBean(form, BusinessPagePayParam.class);
        param.setAccountId(businessId);
        String page = businessPaymentService.businessPagePay(param);
        return Result.getResult(String.class).setMessage("支付页面响应成功").setData(page);
    }

    /**
     * 商家入驻缴费异步回调接口
     * @return
     * @throws Exception
     */
    @PostMapping("/paySuccess")
    @ApiOperation("异步回调接口")
    public String paySuccess() throws Exception {
        Map<String, String[]> parameterMap = WebUtil.getRequest().getParameterMap();
        Map<String, String> params = AlipayUtil.parseParams(parameterMap);
        businessPaymentService.paySuccess(params);
        return "SUCCESS";
    }



}

